package com.gifts.service;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;
import com.google.gson.*;


public class FilterInterests {
	final static JsonParser parser= new JsonParser();
	private ArrayList<String> results;


	public FilterInterests(String accessToken, String userID){
		//e.g. https://graph.facebook.com/217655/likes?access_token=AAACEdEose0cBAC8M3apL9r3BZAKwJVLv0FTF768EuQEMTZB3qy47CDvdNdD5W3G2X5ZB6GCLU3B3s7udCD3VXcsDbCtwMvJj67BjlUm3gZDZD
		String url=	URL_DEFAULT + userID + "/likes?access_token="+accessToken;
		final JsonElement jsonElement=parser.parse(getHTML(url));
		results=filterLikes(jsonElement);
//		if (results.isEmpty()){
//			   results.add("gift card");
//		}
		
	}
	
	public ArrayList<String> getInterests(){
		return results;
	}
	
	private  ArrayList<String> filterLikes(JsonElement likesResponse){
		ArrayList<String> all_likes=new ArrayList<String>();
		ArrayList<String> lessDesirables=new ArrayList<String>();

		//final JsonParser parser= new JsonParser();
		//final JsonElement jsonElement=parser.parse(likesResponse);
		final JsonObject jsonObj=likesResponse.getAsJsonObject();
		if (jsonObj.has("data")){
			if (jsonObj.get("data").isJsonArray()){
				JsonArray jArr=jsonObj.get("data").getAsJsonArray();
				for(JsonElement j:jArr){
					if (j.isJsonObject()){
						if (j.getAsJsonObject().has("id")){
							String tempID=j.getAsJsonObject().get("id").getAsString();
							if(passID(tempID)>0)
								all_likes.add(j.getAsJsonObject().get("name").getAsString());
							else if (passID(tempID)==0)
								lessDesirables.add(j.getAsJsonObject().get("name").getAsString());
						}
					}
				} 
			}
		}
		//if there are not many likes from the categories in the validList 
		//we can the ones from the less desired categories
		if (all_likes.size()<LIST_MIN)	
			all_likes.addAll(lessDesirables);
		return all_likes;
	}
	private int passID(String id){

		//sorted lists
		String[] validList={"Actor/director","Artist","Athlete","Author","Book","Comedian","Games/toys","Movie","Musician/band","Professional sports team","Public figure","Tv show"};
		String[] lessDesirable={"Interest","Music","Musical genre","Sport"};
		String link="https://graph.facebook.com/"+id;
		String response=getHTML(link);
		final JsonParser parser= new JsonParser();
		final JsonElement jsonElement=parser.parse(response);
		if (jsonElement.isJsonObject()){
			final JsonObject jsonObj=jsonElement.getAsJsonObject();
			if(jsonObj.has("category")){
				String category=jsonObj.get("category").getAsString();
				//if the category matches any element in validList
				if (Arrays.binarySearch(validList, category)>=0){
					if (category.equals("Tv show")){	//higher threshold for tvshow
						if(jsonObj.get("likes").getAsInt()>TV_THRESHOLD)
							return 1;
					}
					else if (jsonObj.get("likes").getAsInt()>THRESHOLD)
						return 1;
				}
				//if the category matches any element in lessDesirable list
				else if (Arrays.binarySearch(lessDesirable, category)>=0){
					if(jsonObj.get("likes").getAsInt()>INTEREST_THRESHOLD)
						return 0;
				}
			}
		}
		return -1;
	}

	private String getHTML(String link){
		String line;
		String result = "";
		try {
			URL url=new URL(link);
			HttpURLConnection conn=(HttpURLConnection) url.openConnection();
			conn.setRequestMethod("GET");
			BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
			while ((line=br.readLine())!=null){
				result+=line;
			}
			br.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	private static final String URL_DEFAULT="https://graph.facebook.com/";
	private static final int TV_THRESHOLD=100000;
	private static final int INTEREST_THRESHOLD=10000;
	private static final int THRESHOLD=30000;
	private static final int LIST_MIN=4;

}
